package com.example.demo.common.com.example.demo;

import cn.hutool.core.thread.ThreadUtil;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

public class Demo06 {


    public static void main(String[] args) throws InterruptedException {

        Integer t1 = 1;
        Integer t4 = 888;

        Integer i1 = new Integer(555);
        WeakReference<Integer> w1 = new WeakReference<>(i1);
        i1 = null;
        WeakReference<Integer> w2 = new WeakReference<>(666);

        System.gc();
        ThreadUtil.sleep(3000);

        System.out.println(t1);
        System.out.println(t4);

        System.out.println("==============================");

        System.out.println(w1);
        System.out.println(w2);

        System.out.println("==============================");

        System.out.println(w1.get());
        System.out.println(w2.get());

        System.out.println("==============================");

        Map<WeakReference<Integer>, WeakReference<Integer>> map = new HashMap<>(8);
        WeakReference<Integer> key = new WeakReference<>(666);
        WeakReference<Integer> value = new WeakReference<>(777);
        map.put(key, value);

        Thread.sleep(1000);
        System.gc();
        System.out.println("get " + key.get());
        System.out.println("get " + map.get(key).get());

        System.out.println("==============================");

        Map<WeakReference<Integer>, Integer> map1 = new HashMap<>(8);
        WeakReference<Integer> k1 = new WeakReference<>(666);
        Integer v1 = new Integer(111);
        map1.put(k1, v1);

        Thread.sleep(1000);
        System.gc();
        System.out.println("get " + k1.get());
        System.out.println("get " + map1.get(k1));

    }
}
